﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bank
{
    //信用账户类，继承Account类，实现ISave、IWithdraw接口
    public class CreditAccount:Account, ISave, IWithdraw
    {
        private int totalCredit;    //最大信用额度
        private int restCredit;    //剩余信用额度
        private DateTime payDate;    //还款时间

        //还款时间属性
        public DateTime PayDaate    
        {
            get
            {
                return payDate;
            }
            set
            {
                payDate = value;
            }
        }

        public CreditAccount(int ID, int mon, int pwd, int tCred, int rCred, DateTime payday):base(ID, mon, pwd)
        {
            totalCredit = tCred;
            restCredit = rCred;
            payDate = payday;
        }

        public DateTime getPayD()
        {
            return payDate;
        }

        public int getTotalCredit()
        {
            return totalCredit;
        }

        public int getRestCredit()
        {
            return restCredit;
        }

        public DateTime getPayDate()
        {
            return payDate;
        }

        //override父类方法/实现接口方法，取款成功返回true，否则返回false
        public override bool withdrawMoney(int quan)
        {
            //存款+剩余信用额度足够取款
            if (Money + restCredit >= quan)
            {
                //当存在存款，首先取出存款
                if (Money > 0)
                {
                    //当存款足够，则使用存款
                    if (Money > quan)
                        Money -= quan;
                    //当存款不足，则使用存款以及信用额度
                    else
                    {
                        restCredit -= (quan - Money);
                        Money = 0;
                    }
                }
                //当没有存款，直接使用信用额度
                else
                    restCredit -= quan;
                return true;
            }
            //不足以取款则返回false
            else
                return false;
        }

        //override父类方法/实现接口方法
        public override void saveMoney(int quan)
        {
            //当存在欠款，则优先还款
            if(restCredit < totalCredit)
            {
                //当欠款大于等于该笔存款金额时，进行还款
                if (restCredit + quan <= totalCredit)
                    restCredit += quan;
                //当欠款小于该笔存款金额时，除进行还款外，将多余的部分存入存款
                else
                {
                    Money += quan - (totalCredit - restCredit);
                    restCredit = totalCredit;
                }
            }
            //当没有欠款，则直接全部存入存款
            else
            {
                Money += quan;
            }
        }
    }
}
